<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Auction
 * @version 	: 1.1
 * COPYRIGHT    : 2010
 * ------------------------------------------------------
 *
 * Created on	:  Sep 13, 2010
 *
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
RClassesImporter::cls('controllers.AbstractBackEndController');

/**
 * The controller for State application
 *
 */
class RegisterController extends AbstractBackEndController {
	
	var $_model;
	
	function __construct() {
		parent::__construct();
		SysApplication::importModelCls('ContactModel');
		$this->_model = new ContactModel();
		$this->registerTask ( "new", "edit" );
		$this->registerTask ( "apply", "save" );
	}
	
	function display() {
		$this->setTitle ( "Contact management" );
		//$this->addToolbarButton ( "new" );
		//$this->addToolbarButton ( "edit" );
		$this->addToolbarButton ( "delete" );
		$msg = SysRequest::getVar ( "msg" );
		RCoreImporter::cls( "html.TableControl" );
		RClassesImporter::cls("dbo.dtos.ContactDTO" );		
		$table = new TableControl ( 'ContactDTO', NULL, $_REQUEST, 'ContactCtrl', '', '' );		
		$nextExec = $this->getExecuteBy();
		$nextExec = ucfirst($nextExec);
		
		$table->setLinkRule("subject",'javascript:edit'.$nextExec.'Item($key)');		
		
		$this->_setCommonFields($table);
		$table->removeField('contact_id');

		$msg = "<p class=\"form-message\" style=\"text-align:left;\">
					{$msg}
			  </p>";
		$tblHtml = $table->createHtmlTable(20);
		eval('echo "'.SysApplication::loadTemplate('back/listContact').'";');	
	}
	
	
	function _setCommonFields(&$fieldsCtrl) {
		//$fieldsCtrl->removeField('deleted');
		//$fieldsCtrl->setFieldHtmlInfo('icon', $this->_getImageFieldInfo('icon'));		
	}
	

	function edit($dto = null) {
		$oModel = $this->_model;
		$cid = SysRequest::getVar ( "cid" );
		if (empty( $dto )) { 
			if(is_array($cid)) {
				$cid = $cid[0];
			}				
			$dto = $oModel->getContactDetails($cid);
		}
		
		
		RCoreImporter::cls( "html.FormFieldsControl" );			
		$formData = new FormFieldsControl ( $dto );	

		$formData = $this->_getEditFormAndPrepareEditTask($dto, 'Contact');
		$formData->setFieldHtmlInfo("content", $this->_getRichTextHtmlFieldInfo('content'));
		$htmlContent = $formData->generateHtml();
		
		$msg = SysRequest::getVar('msg');
		eval('echo "'.SysApplication::loadTemplate('back/editContact').'";');	
	}

	
	function save($isContactEdit = false) {		
		$model = $this->_model;		
		$dto = new ContactDTO();
		if (SysRequest::getInt('id')) {
			$dto->resetToNull();
		}
		$dto->bind($_POST);
		if ($dto->checkKeyNotDefault()) {
			$dto->setEmptyNumberToZero();
		}
			
		$picField = $this->_getPicFileFiledInfo('image'); 
		$dto->image = $picField->getUploadedFilePath('files/images/news/');	
		
		// files processing
		//$this->_setFileValue('icon', $dto, 'files/images/category/thumbnail/', $_POST);
		
		$result = $model->saveContactDTO ( $dto );										
		if ($dto->hasError())
			$msg = $dto->getAllErrorsMsg();			

		if ($result === false || $this->getExecuteVal () == 'apply') {
			$_REQUEST ["msg"] = $msg ? $msg : SysLanguage::_ ( "ITEM_SAVE_SUCCESSFUL" );
			return $this->edit ($dto);
		} else {						
			$this->setRedirectToFunction('display', $msg ? $msg : SysLanguage::_ ( "ITEM_SAVE_SUCCESSFUL" ));			
		}				
	}
	function apply($isContactEdit = false) {		
		$model = $this->_model;		
		$dto = new ContactDTO();
		if (SysRequest::getInt('id')) {
			$dto->resetToNull();
		}
		$dto->bind($_POST);
		if ($dto->checkKeyNotDefault()) {
			$dto->setEmptyNumberToZero();
		}
			
		$picField = $this->_getPicFileFiledInfo('image'); 
		$dto->image = $picField->getUploadedFilePath('files/images/news/');	
		
		// files processing
		//$this->_setFileValue('icon', $dto, 'files/images/category/thumbnail/', $_POST);
		
		$result = $model->saveContactDTO ( $dto );										
		if ($dto->hasError())
			$msg = $dto->getAllErrorsMsg();			

		if ($result === false || $this->getExecuteVal () == 'apply') {
			$_REQUEST ["msg"] = $msg ? $msg : SysLanguage::_ ( "ITEM_SAVE_SUCCESSFUL" );
			return $this->edit ($dto);
		}
	}

	function delete() {
		$model = $this->_model;
        $arrayIds = SysRequest::getVar ( "cid" );
        $model->delete($arrayIds);
        $this->setRedirectToFunction('display', SysLanguage::_ ( "ITEM_DELETE_SUCCESSFUL" ));
	}

	function &_getPicFileFiledInfo($inputName) {
		
		RCoreImporter::cls( "html.fieldInfo.FlashFileHtmlFieldInfo" );
		$field = new FlashFileHtmlFieldInfo($inputName,$linkRule);
		$field->setSupportedFilteType('.jpg,.jpeg,.bmp,.png,.gif');
		return $field;
	}
}